import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:myflutterproject/shoppinglistitem.dart';

/*整合例子1*/
class BaseConformityDemo extends StatefulWidget {
  final List<Product> products;
  BaseConformityDemo({ required this.products}) ;
  @override
  State<StatefulWidget> createState() => BaseConformityState();
}

class BaseConformityState extends State<BaseConformityDemo> {
  Set<Product> _shoppingCart =new Set<Product>();
  void _handleCartChanged(Product product,bool inCart){
    setState(() {
      if(inCart){
        _shoppingCart.add(product);
      }else{
        _shoppingCart.remove(product);
      }

    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("购物车"),
        centerTitle: true,
      ),
      body: ListView(
        padding: EdgeInsets.symmetric(vertical: 8.0),
        children: widget.products.map((Product product){
          return ShoppingListItemDemo(
            product:product,
            inCart: _shoppingCart.contains(product),
            onCartChanged:_handleCartChanged,
          );
        }).toList(),
      ),
    );
  }
}

void main(){
  /*runApp(
    MaterialApp(
      title: '购物车',
      home: BaseConformityDemo(
        products: <Product>[
          Product(name:'1'),
          Product(name:'2'),
          Product(name:'3'),
          Product(name:'4'),
          Product(name:'5'),
          Product(name:'6'),
          Product(name:'7')
        ],
      ),
    ),
  );*/

}